using System;
using System.IO;
using System.Text;
using System.Web;
using log4net;
using Wikey.Extensibility.Interfaces;

namespace Wikey.Domain.Components
{
    public class Logger : ILogger
    {
        private ILog log = LogManager.GetLogger("Wikey");

        public void Debug(string message)
        {
            InternalWrite(message);
        }

        public void Debug(string message, Exception ex)
        {
            InternalWrite(message);
        }

        public void Error(string message, Exception ex)
        {
            InternalWrite(String.Concat( message,  ErrorHandler(ex)));
            
        }

        public void Warn(string message)
        {
            InternalWrite(message);
        }

        public void Warn(string message, Exception ex)
        {
            InternalWrite(String.Concat(message, ErrorHandler(ex)));
            
        }

        public void Trace(string message)
        {
            InternalWrite(message);
        }

        private void InternalWrite( string message )
        {
            log.Info(message);
        }

        private static string ErrorHandler( Exception ex )
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendFormat("Message:{0}", ex.Message);
            stringBuilder.AppendFormat("Stack: {0}", ex.StackTrace);
            stringBuilder.AppendFormat("Source: {0}", ex.Source);

            if (ex.InnerException != null)
                stringBuilder.AppendFormat("Inner Exception:{0}", ErrorHandler(ex));

            return stringBuilder.ToString();
        }
    }
}